home *** CD-ROM | disk | FTP | other *** search
/ World of Video / World of Video.iso / gfxprograms / 3dprograms / t3dlib / source / writevort.c < prev   
C/C++ Source or Header  |  1995-02-13  |  14KB  |  520 lines

  1. /* writevort.c - dump the internal database to a Vort input file
  2.  *             - written by Glenn M. Lewis - 10/11/91
  3.  */
  4.  
  5. static char rcs_id[] = "$Id: writevort.c,v 1.13 1993/01/30 23:43:03 glewis Exp $";
  6.  
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include "t3dlib.h"
  10. #ifdef __STDC__
  11. #include <stdlib.h>
  12. #include <strings.h>
  13. #include "writevort_protos.h"
  14. #endif
  15.  
  16. static void process_DESC();
  17. static void process_EXTR();
  18. static void process_INFO();
  19. static void process_OBJ();
  20.  
  21. /* Two-space tabs */
  22. #define TABSTOP "  "
  23.  
  24. static FILE *out;
  25. static char tab[133];
  26. static int num_OBJ, num_DESC, num_TOBJ;
  27. static int cur_level, cur_objnum;
  28. static int prntline;
  29.  
  30. static struct save_hier {
  31.     int objnum;
  32.     struct save_hier *next;
  33. } *root = 0;
  34. typedef struct save_hier HIER;
  35.  
  36. /* Here are a few necessary utilities */
  37.  
  38. static void indent()
  39. {
  40.     strcat(tab, TABSTOP);
  41. }
  42.  
  43. static void outdent()
  44. {
  45.     register int i = strlen(tab) - strlen(TABSTOP);
  46.     if (i<0) {
  47.         fprintf(stderr, "Whoa, Glenn!  You blew it!\n");
  48.         tab[0] = '\0';
  49.         return;
  50.     }
  51.     tab[i] = '\0';
  52. }
  53.  
  54. static void send_XYZ(f)            /* Print a common string */
  55. XYZ_st *f;
  56. {
  57.     fprintf(out, " %.12g,%.12g,%.12g ", f->x, f->y, f->z);
  58. }
  59.  
  60. static void send_RGB(rgb)            /* Print a common string */
  61. RGB_st *rgb;
  62. {
  63.     fprintf(out, " %.12g,%.12g,%.12g ",
  64.         ((double)rgb->r)/255.0,
  65.         ((double)rgb->g)/255.0,
  66.         ((double)rgb->b)/255.0);
  67. }
  68.  
  69. /********************/
  70. /* The MAIN section */
  71. /********************/
  72.  
  73. int write_Vort(world, file)
  74. WORLD *world;
  75. FILE *file;
  76. {
  77.     register OBJECT *o;
  78.  
  79.     if (!(out=file) || !world) return(0);    /* File not open */
  80.  
  81.     tab[0] = '\0';
  82.     num_OBJ = num_DESC = num_TOBJ = 0;
  83.  
  84.     fputs("/* TTDDD library - Written by Glenn M. Lewis - 10/11/91 */\n\n",out);
  85.     fputs("maxhitlevel 1\n", out);
  86.     fputs("light {\n  color 1,1,1\n  direction(-1,1,-1)\n}\n", out);
  87.     fputs("up(0,0,1)\nlookat(10,-100,100,0,0,0,0)\n", out);
  88.  
  89.     if (world->info) process_INFO(world->info);
  90.     for (o=world->object; o; o=o->next)
  91.         process_OBJ(o);
  92.     return(1);
  93. }
  94.  
  95. static void process_INFO(info)
  96. INFO *info;
  97. {
  98.     fprintf(out, "%s/* INFO Begin */\n", tab);
  99.     indent();
  100. /*
  101.     for (i=0; i<8; i++)
  102.         if (info->brsh[i][0])
  103.             fprintf(out, "%sBRSH[%d]=\"%s\"\n", tab, i, info->brsh[i]);
  104.  
  105.     for (i=0; i<8; i++)
  106.         if (info->stnc[i][0])
  107.             fprintf(out, "%sSTNC[%d]=\"%s\"\n", tab, i, info->stnc[i]);
  108.  
  109.     for (i=0; i<8; i++)
  110.         if (info->txtr[i][0])
  111.             fprintf(out, "%sTXTR[%d]=\"%s\"\n", tab, i, info->txtr[i]);
  112. */
  113.  
  114.     if (info->obsv) {
  115.         fprintf(out, "%slookat(", tab); send_XYZ(&info->obsv->came);
  116.         fprintf(out, "%s/* OBSV Rotate", tab); send_XYZ(&info->obsv->rota);
  117.         fprintf(out, "%s   focaldist  %.12g */\n", tab, info->obsv->foca);
  118.         fprintf(out, "%s)", tab);
  119.     }
  120.  
  121. /*    if (info->otrk[0]) fprintf(out, "%sOTRK \"%s\"\n", tab, info->otrk);    */
  122.  
  123. /*
  124.     if (info->ostr) {
  125.         if (info->ostr->path[0])
  126.             fprintf(out, "%sOSTR Path \"%s\"\n", tab, info->ostr->path);
  127.         fprintf(out, "%sOSTR Translate", tab); send_XYZ(&info->ostr->tran);
  128.         fprintf(out, "%sOSTR Rotate   ", tab); send_XYZ(&info->ostr->rota);
  129.         fprintf(out, "%sOSTR Scale    ", tab); send_XYZ(&info->ostr->scal);
  130.         i = info->ostr->info;
  131.         strin[0] = '\0';
  132.         if (i&(1<<0))  strcat(strin, " ABS_TRA");
  133.         if (i&(1<<1))  strcat(strin, " ABS_ROT");
  134.         if (i&(1<<2))  strcat(strin, " ABS_SCL");
  135.         if (i&(1<<4))  strcat(strin, " LOC_TRA");
  136.         if (i&(1<<5))  strcat(strin, " LOC_ROT");
  137.         if (i&(1<<6))  strcat(strin, " LOC_SCL");
  138.         if (i&(1<<8))  strcat(strin, " X_ALIGN");
  139.         if (i&(1<<9))  strcat(strin, " Y_ALIGN");
  140.         if (i&(1<<10)) strcat(strin, " Z_ALIGN");
  141.         if (i&(1<<12)) strcat(strin, " FOLLOW_ME");
  142.         fprintf(out, "%sOSTR Info%s\n", tab, strin);
  143.     }
  144. */
  145.  
  146.     if (info->fade) {
  147.         fprintf(out, "%s/* FADE FadeAt %.12g*/\n", tab, info->fade->at);
  148.         fprintf(out, "%s/* FADE FadeBy %.12g*/\n", tab, info->fade->by);
  149.         fprintf(out, "%s/* FADE FadeTo", tab); send_RGB(&info->fade->to);
  150.         fprintf(out, "*/\n");
  151.     }
  152.  
  153.     if (info->skyc) {
  154.         fprintf(out, "%s/* SKYX Horizon", tab); send_RGB(&info->skyc->hori);
  155.         fprintf(out, "*/\n");
  156.         fprintf(out, "%s/* SKYC Zenith ", tab); send_RGB(&info->skyc->zeni);
  157.         fprintf(out, "*/\n");
  158.     }
  159.  
  160.     if (info->ambi)
  161.         { fprintf(out, "%sbackground", tab); send_RGB(info->ambi); }
  162.  
  163. /*
  164.     if (info->glb0)
  165.         for (i=0; i<8; i++)
  166.             fprintf(out, "%sGLB0[%d]=%u\n", tab, i, info->glb0[i]);
  167. */
  168.  
  169.     outdent();
  170.     fprintf(out, "%s/* End INFO */\n", tab);
  171. }
  172.  
  173. static void process_OBJ(obj)
  174. register OBJECT *obj;
  175. {
  176.     register HIER *p;
  177.     num_OBJ++;
  178.     fprintf(out, "%s/* OBJ Begin \"Hierarchy %d\" */\n", tab, num_OBJ);
  179.     num_DESC = num_TOBJ = 0;        /* Reset counters */
  180.     cur_level = 0;
  181.     cur_objnum = 1;
  182.     prntline = 1;
  183.  
  184.     if (obj->extr) process_EXTR(obj->extr);
  185.     else process_DESC(obj);
  186.  
  187.     while (root) {                /* This should happen at most once. */
  188.         p = root->next;
  189.         free((char *)root);        /* Delete this from list */
  190.         root = p;
  191.         outdent();
  192.     }
  193.  
  194.     fprintf(out, "%s/* End OBJ   \"Hierarchy %d\" */\n", tab, num_OBJ);
  195. }
  196.  
  197. static void process_TOBJ()
  198. {
  199.     register HIER *p;
  200.     if (num_DESC-num_TOBJ < cur_level) {    /* Pop old level off HIER */
  201.         cur_level--;
  202.         cur_objnum = root->objnum;
  203.         p = root->next;
  204.         free((char *)root);    /* Delete from list */
  205.         root = p;
  206.         outdent();    /* Pretty file formatting */
  207.     }
  208.     fprintf(out, "%s}\n", tab);
  209.     fprintf(out, "%s/* TOBJ       \"Object %d at level %d of hierarchy %d\" */\n",
  210.         tab, cur_objnum-1, num_DESC-num_TOBJ, num_OBJ);
  211.     num_TOBJ++;
  212.     prntline = 1;
  213. }
  214.  
  215. static void process_EXTR(extr)
  216. EXTR *extr;
  217. {
  218.     if (!prntline) fprintf(out, "\n");    /* Print one anyway */
  219.     indent();
  220.     num_DESC++;
  221.  
  222.     fprintf(out, "%s/* EXTR Begin \"Object %d at level %d of hierarchy %d\" */\n",
  223.         tab, cur_objnum, num_DESC-num_TOBJ, num_OBJ);
  224.     fprintf(out, "%scomposite {\n", tab);
  225.     indent();
  226.     fprintf(out, "%s/* LOAD \"%s\" */\n", tab, extr->filename);
  227.     fprintf(out, "include %s\n", extr->filename);
  228.     fprintf(out, "%stranslate(", tab); send_XYZ(&extr->mtrx.tran);
  229.     fprintf(out, ")\n%sscale    (", tab); send_XYZ(&extr->mtrx.scal);
  230.     fprintf(out, "\n%stransform {\n", tab);
  231.     indent();
  232.     fprintf(out, "%s%.12g,%.12g,%.12g,",  tab,
  233.         extr->mtrx.rota1.x,
  234.         extr->mtrx.rota1.y,
  235.         extr->mtrx.rota1.z);
  236.     fprintf(out, "%s%.12g,%.12g,%.12g,",  tab,
  237.         extr->mtrx.rota2.x,
  238.         extr->mtrx.rota2.y,
  239.         extr->mtrx.rota2.z);
  240.     fprintf(out, "%s%.12g,%.12g,%.12g\n", tab,
  241.         extr->mtrx.rota3.x,
  242.         extr->mtrx.rota3.y,
  243.         extr->mtrx.rota3.z);
  244.     outdent();
  245.     fprintf(out, "%s}\n", tab);
  246.  
  247.     outdent();
  248.     fprintf(out, "%s}\n", tab);
  249.     fprintf(out, "%s/* End EXTR   \"Object %d at level %d of hierarchy %d\" */\n",
  250.         tab, cur_objnum, num_DESC-num_TOBJ, num_OBJ);
  251.  
  252.     num_TOBJ++;
  253.     cur_objnum++;
  254.     outdent();
  255.     prntline = 1;
  256. }
  257.  
  258. static void process_DESC(object)
  259. OBJECT *object;
  260. {
  261.     register int i;
  262.     register HIER *p;
  263.     register OBJECT *obj;
  264.     register DESC *desc = object->desc;
  265.     register int p1, p2, p3;
  266.  
  267.     num_DESC++;
  268.     if (num_DESC-num_TOBJ > cur_level) {    /* Push new level in HIER */
  269.         if (!prntline) fprintf(out, "\n");    /* Print one anyway */
  270.         if (!(p = (HIER*)malloc(sizeof(HIER)))) {
  271.             fprintf(stderr, "ERROR!  Out of memory.\n*** ABORT ***\n");
  272.             exit(20);
  273.         }
  274.         p->next = root;        /* Insert into list */
  275.         root = p;
  276.         root->objnum = cur_objnum;
  277.         cur_level++;
  278.         cur_objnum = 1;
  279.         indent();    /* Pretty file formatting */
  280.     }
  281.  
  282.     fprintf(out, "%s/* DESC Begin \"Object %d at level %d of hierarchy %d\" */\n",
  283.         tab, cur_objnum, num_DESC-num_TOBJ, num_OBJ);
  284. /*    fprintf(out, "%sgrid 20 20 20\n", tab);    */
  285.     fprintf(out, "%scomposite {\n", tab);
  286.     indent();
  287.  
  288.     if (desc->name[0]) fprintf(out, "%s/* name \"%s\" */\n", tab, desc->name);
  289.  
  290.     if (desc->posi) {
  291.         fprintf(out, "%stranslate(", tab);
  292.         send_XYZ(desc->posi);
  293.         fprintf(out, ")\n");
  294.     }
  295.  
  296.     if (desc->colr) {
  297.         fprintf(out, "%sambient", tab);
  298.         send_RGB(desc->colr);
  299.         fprintf(out, "\n");
  300.     }
  301.  
  302.     if (desc->refl) {
  303.         fprintf(out, "%s/* specular", tab);
  304.         send_RGB(desc->refl);
  305.         fprintf(out, "*/\n");
  306.     }
  307.  
  308.     if (desc->tran) { fprintf(out, "%s/* TRAN", tab); send_RGB(desc->tran);
  309.         fprintf(out, "*/\n");
  310.     }
  311.  
  312. #ifdef WRITETTDDD
  313.     if (desc->fcount) {
  314.         fprintf(out, "%sCLST Count %u\n", tab, i);
  315.         for (i=0; i<desc->fcount; i++) {
  316.             if (desc->colr) {
  317.                 if (desc->clst[i*3  ]==desc->colr->r &&
  318.                     desc->clst[i*3+1]==desc->colr->g &&
  319.                     desc->clst[i*3+2]==desc->colr->b) continue; /* Skip */
  320.             } else {
  321.                 if (desc->clst[i*3  ]==255 &&
  322.                     desc->clst[i*3+1]==255 &&
  323.                     desc->clst[i*3+2]==255) continue;    /* Skip this one */
  324.             }
  325.             fprintf(out, "%sCLST Color[%u]", tab, i);
  326.             send_RGB((RGB_st*)&desc->clst[i*3]);
  327.         }
  328.         fprintf(out, "%sRLST Count %u\n", tab, i);
  329.         for (i=0; i<desc->fcount; i++) {
  330.             if (desc->refl) {
  331.                 if (desc->rlst[i*3  ]==desc->refl->r &&
  332.                     desc->rlst[i*3+1]==desc->refl->g &&
  333.                     desc->rlst[i*3+2]==desc->refl->b) continue; /* Skip */
  334.             } else {
  335.                 if (desc->rlst[i*3  ]==0 &&
  336.                     desc->rlst[i*3+1]==0 &&
  337.                     desc->rlst[i*3+2]==0) continue;    /* Skip this one */
  338.             }
  339.             fprintf(out, "%sRLST Color[%u]", tab, i);
  340.             send_RGB((RGB_st*)&desc->rlst[i*3]);
  341.         }
  342.         fprintf(out, "%sTLST Count %u\n", tab, i);
  343.         for (i=0; i<desc->fcount; i++) {
  344.             if (desc->tran) {
  345.                 if (desc->tlst[i*3  ]==desc->tran->r &&
  346.                     desc->tlst[i*3+1]==desc->tran->g &&
  347.                     desc->tlst[i*3+2]==desc->tran->b) continue; /* Skip */
  348.             } else {
  349.                 if (desc->tlst[i*3  ]==0 &&
  350.                     desc->tlst[i*3+1]==0 &&
  351.                     desc->tlst[i*3+2]==0) continue;    /* Skip this one */
  352.             }
  353.             fprintf(out, "%sTLST Color[%u]", tab, i);
  354.             send_RGB((RGB_st*)&desc->tlst[i*3]);
  355.         }
  356.     }
  357. #endif
  358.  
  359.     if (desc->tpar) {
  360.         for (i=0; i<16; i++)
  361.             fprintf(out, "%s/* TPAR[%u]=%.12g */\n", tab, i, desc->tpar[i]);
  362.     }
  363.  
  364.     if (desc->surf) {
  365.         for (i=0; i<5; i++)
  366.             fprintf(out, "%s/* SURF[%u]=%d */\n", tab, i, desc->surf[i]);
  367.     }
  368.  
  369.     if (desc->mttr) {
  370.         fprintf(out, "%s/* MTTR Type =%u */\n", tab, desc->mttr->type);
  371.         fprintf(out, "%s/* MTTR Index=%.12g */\n", tab, (double)desc->mttr->indx);
  372.     }
  373.  
  374.     if (desc->spec) {
  375.         fprintf(out, "%s/* SPEC Spec=%u */\n", tab, desc->spec[0]);
  376.         fprintf(out, "%s/* SPEC Hard=%u */\n", tab, desc->spec[1]);
  377.     }
  378.  
  379.     if (desc->prp0) {
  380.         for (i=0; i<6; i++)
  381.             fprintf(out, "%s/* PRP0[%u]=%u */\n", tab, i, desc->prp0[i]);
  382.     }
  383.  
  384.     if (desc->ints)
  385.         fprintf(out, "%s/* INTS=%.12g */\n", tab, desc->ints);
  386.  
  387. #ifdef WRITETDDD
  388.     if (desc->stry) {
  389.         fprintf(out, "%sSTRY Path \"%s\"\n", tab, desc->stry->path);
  390.         fprintf(out, "%sSTRY Translate", tab); send_XYZ(&desc->stry->tran);
  391.         fprintf(out, "%sSTRY Rotate   ", tab); send_XYZ(&desc->stry->rota);
  392.         fprintf(out, "%sSTRY Scale    ", tab); send_XYZ(&desc->stry->scal);
  393.         i = desc->stry->info;
  394.         strin[0] = '\0';
  395.         if (i&(1<<0))  strcat(strin, " ABS_TRA");
  396.         if (i&(1<<1))  strcat(strin, " ABS_ROT");
  397.         if (i&(1<<2))  strcat(strin, " ABS_SCL");
  398.         if (i&(1<<4))  strcat(strin, " LOC_TRA");
  399.         if (i&(1<<5))  strcat(strin, " LOC_ROT");
  400.         if (i&(1<<6))  strcat(strin, " LOC_SCL");
  401.         if (i&(1<<8))  strcat(strin, " X_ALIGN");
  402.         if (i&(1<<9))  strcat(strin, " Y_ALIGN");
  403.         if (i&(1<<10)) strcat(strin, " Z_ALIGN");
  404.         if (i&(1<<12)) strcat(strin, " FOLLOW_ME");
  405.         fprintf(out, "%sSTRY Info%s\n", tab, strin);
  406.     }
  407. #endif
  408.  
  409.     if (desc->shap) {
  410.         fprintf(out, "%s/* SHAP Shape = %u */\n", tab, desc->shap[0]);
  411.         if (desc->shap[0]==0 && !desc->shap[1]) {    /* Sphere */
  412.             fprintf(out, "%ssphere {\n", tab);
  413.             indent();
  414.             fprintf(out, "%scenter(", tab);
  415.             send_XYZ(desc->posi);
  416.             fprintf(out, ")\n%sradius %.12g\n", tab, desc->size->x);
  417.             outdent();
  418.             fprintf(out, "%s}\n", tab);
  419.             goto SKIP_REST;
  420.         }
  421.         fprintf(out, "%s/* SHAP Lamp  = %u */\n", tab, desc->shap[1]);
  422.         if (desc->shap[1]<2) {
  423.             fprintf(out, "%slight {\n", tab);
  424.             indent();
  425.             if (desc->colr) {
  426.                 fprintf(out, "%scolor", tab);
  427.                 send_RGB(desc->colr);
  428.                 fprintf(out, "\n");
  429.             }
  430.             if (desc->shap[1]==1)    /* Sunlight */
  431.                 fprintf(out, "%sdirection(%.12g,%.12g,%.12g)\n", tab,
  432.                     -(desc->posi->x),
  433.                     -(desc->posi->y),
  434.                     -(desc->posi->z));
  435.             else    /* Like a lamp */
  436.                 fprintf(out, "%slocation(%.12g,%.12g,%.12g)\n", tab,
  437.                     desc->posi->x,
  438.                     desc->posi->y,
  439.                     desc->posi->z);
  440.             outdent();
  441.             fprintf(out, "%s}\n", tab);
  442.             goto SKIP_REST;
  443.         }
  444.     }
  445.  
  446. /*
  447.     if (desc->axis) {
  448.         fprintf(out, "%sAXIS XAxis", tab); send_XYZ(&desc->axis->xaxi);
  449.         fprintf(out, "%sAXIS YAxis", tab); send_XYZ(&desc->axis->yaxi);
  450.         fprintf(out, "%sAXIS ZAxis", tab); send_XYZ(&desc->axis->zaxi);
  451.     }
  452.  
  453.     if (desc->size)
  454.         { fprintf(out, "%sSIZE", tab); send_XYZ(desc->size); }
  455.  
  456.     if (desc->pcount) {
  457.         fprintf(out, "%sPNTS PCount %u\n", tab, desc->pcount);
  458.         for (i=0; i<desc->pcount; i++) {
  459.             fprintf(out, "%sPNTS Point[%d]", tab, i);
  460.             send_XYZ(&desc->pnts[i]);
  461.         }
  462.     }
  463.  
  464.     if (desc->ecount) {
  465.         fprintf(out, "%sEDGE ECount %u\n", tab, desc->ecount);
  466.         for (i=0; i<desc->ecount; i++) {
  467.             fprintf(out, "%sEDGE Edge[%d] %u %u\n", tab, i,
  468.                 desc->edge[i<<1], desc->edge[(i<<1)+1]);
  469.         }
  470.     }
  471. */
  472.  
  473.     if (desc->fcount && desc->ecount && desc->pcount) {
  474.         fprintf(out, "%s/* FACE TCount %u */\n", tab, desc->fcount);
  475.         for (i=0; i<desc->fcount; i++) {
  476. /*            fprintf(out, "%sFACE Connect[%u] %u %u %u\n", tab, i,
  477.                 desc->face[i*3], desc->face[i*3+1], desc->face[i*3+2]);
  478. */
  479.         /* First check to make sure that this triangle is real */
  480.             p1 = desc->edge[(desc->face[i*3])<<1];
  481.             p2 = desc->edge[((desc->face[i*3])<<1)+1];
  482.             if (p1 == p2) continue;    /* How did *this* happen? */
  483.             p3 = desc->edge[(desc->face[i*3+2])<<1];
  484.             if (p1 == p3 || p2 == p3)
  485.                 p3 = desc->edge[((desc->face[i*3+2])<<1)+1];
  486.             if (p1 == p3 || p2 == p3) continue; /* How did *this* happen? */
  487.         /* Now check the actual points for equality */
  488.             if (bcmp(&desc->pnts[p1], &desc->pnts[p2], sizeof(XYZ_st))==0 ||
  489.                 bcmp(&desc->pnts[p1], &desc->pnts[p3], sizeof(XYZ_st))==0 ||
  490.                 bcmp(&desc->pnts[p2], &desc->pnts[p3], sizeof(XYZ_st))==0)
  491.                 continue;
  492.  
  493.             fprintf(out, "%spolygon {\n", tab);
  494.             indent();
  495.             fprintf(out, "%svertex(", tab);        send_XYZ(&desc->pnts[p1]);
  496.             fprintf(out, ")\n%svertex(", tab);    send_XYZ(&desc->pnts[p2]);
  497.             fprintf(out, ")\n%svertex(", tab);    send_XYZ(&desc->pnts[p3]);
  498.             outdent();
  499.             fprintf(out, ")\n%s}\n", tab);
  500.         }
  501.     }
  502.  
  503. SKIP_REST:
  504.  
  505.     for (obj=object->child; obj; obj=obj->next) {
  506.         if (obj->extr) process_EXTR(obj->extr);
  507.         else process_DESC(obj);
  508.     }
  509.  
  510.     outdent();
  511.     fprintf(out, "%s/* End DESC   \"Object %d at level %d of hierarchy %d\" */\n",
  512.         tab, cur_objnum, num_DESC-num_TOBJ, num_OBJ);
  513.  
  514.  
  515.     cur_objnum++;
  516.     prntline = 0;
  517.     process_TOBJ();
  518. }
  519.  
  520.